package ai.peanut.selfbase.common.domain;

import ai.peanut.selfbase.common.exception.SystemException;

/**
 * 状态码接口定义
 * <p>
 * 建议业务系统状态码枚举类实现该接口
 * 
 * @author shenghua.hu
 */
public interface StatusDefinition {
	int getErrorCode();

	String getResultCode();
	String getErrorReason();

	default SystemException throwError() {
		throw new SystemException(this.getErrorCode(), this.getResultCode(), this.getErrorReason());
	}

	default SystemException throwError(Throwable throwable) {
		throw new SystemException(this.getErrorCode(), this.getResultCode(), this.getErrorReason()).setCause(throwable);
	}

	/**
	 * 使用传入消息体作为返回给调用者的errorReason
	 * 
	 * @param msg
	 * @return
	 */
	default SystemException throwError(String msg) {
		throw new SystemException(this.getErrorCode(), this.getResultCode(), msg);
	}

	/**
	 * 使用传入消息体作为返回给调用者的errorReason
	 * 
	 * @param msg
	 * @return
	 */
	default SystemException throwError(String msg, Throwable throwable) {
		throw new SystemException(this.getErrorCode(),this.getResultCode(), msg).setCause(throwable);
	}

	/**
	 * 将传入的详细描述追加到默认errorReason后面
	 * <p>
	 * 例如：参数错误(`x`必须为数字类型)
	 * 
	 * @param detail
	 * @return
	 */
	default SystemException throwErrorAndAppendDetail(String detail) {
		throw new SystemException(this.getErrorCode(), this.getResultCode(), this.getErrorReason() + "(" + detail + ")");
	}

	/**
	 * 将传入的详细描述追加到默认errorReason后面
	 * <p>
	 * 例如：参数错误(`x`必须为数字类型)
	 * 
	 * @param detail
	 * @return
	 */
	default SystemException throwErrorAndAppendDetail(String detail, Throwable throwable) {
		throw new SystemException(this.getErrorCode(), this.getResultCode(), this.getErrorReason() + "(" + detail + ")").setCause(throwable);
	}

	/**
	 * 使用状态定义创建DataResult<?>
	 * 
	 * @return
	 */
	default DataResult<?> newDataResult() {
		return DataResult.faild(getErrorCode(),this.getResultCode(), getErrorReason());
	}

	/**
	 * 使用状态定义创建DataResult<?>
	 * 
	 * @param msg 作为Status.statusReason使用
	 * @return
	 */
	default DataResult<?> newDataResult(String msg) {
		return DataResult.faild(getErrorCode(),getResultCode(), msg);
	}
}
